//
//  HZPeiziBuySellView.m
//  Stock_Stategy
//
//  Created by MayerF on 2017/12/14.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZPeiziBuySellView.h"
#import "HZPeiziHeaderView.h"
#import "HZPeiziMoneyFlowCell.h"

@interface HZPeiziBuySellView()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)HZPeiziBuySellTopView *pTopView;
@property (nonatomic, strong)HZPeiziHeaderView *pHeaderView;
@property (nonatomic, strong)UITableView *pTableView;
@property (nonatomic, strong)NSArray *pDataList;
@end

@implementation HZPeiziBuySellView

#pragma mark -
#pragma mark 生命周期
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
        [self initCommon];
        [self initConstraints];
    }
    return self;
}
- (void)initCommon
{
    _pTopView = [HZPeiziBuySellTopView new];
    _pTableView = [UITableView new];
    _pTableView.dataSource = self;
    _pTableView.delegate = self;
    _pTableView.tableFooterView = [UIView new];
    
    _pTableView.bounces = NO;
    _pTableView.backgroundColor = kMYHexToRGB(0xf6f6f6, 1);
    
}
- (void)initConstraints
{
    [self addSubview:_pTopView];
    [self addSubview:_pTableView];
    
    [_pTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.mas_equalTo(self);
        make.height.mas_equalTo(240);
    }];
    [_pTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pTopView.mas_bottom).offset(20);
        make.left.right.bottom.mas_equalTo(self);
    }];
}

- (void)clearBothData
{
    [_pTopView clearBothData];
}
- (void)setBuySellType:(kTradeType)type
{
    _pTopView.nTradeType = type;
}
- (void)updateTopData:(id)data
{
    [_pTopView updateTopData:data];
}
- (void)updateMoneyData:(NSString *)num
{
    [_pTopView updateMoneyData:num];
}
- (void)updateAccountData:(NSArray<HZPeiziTradeStockListModel *> *)data
{
    if (data && data.count > 0) {
        [_pTopView updateAccountData:data];
        
        NSMutableArray *tempArray = [NSMutableArray array];
        for (HZPeiziTradeStockListModel *model in data) {
            HZPeiziMoneyFlowModel *flowModel = [HZPeiziMoneyFlowModel new];
            flowModel.mode_type = kCell_Trade_Type;
            flowModel.mode_one = model.stkname;
            flowModel.mode_two = model.income;
            flowModel.mode_three = model.amt;
            flowModel.mode_four = model.price;
            flowModel.mode_downOne = model.marketval;
            flowModel.mode_downTwo = model.incomeratio;
            flowModel.mode_downThree = model.enable;
            flowModel.mode_downFour = model.lastprice;
            flowModel.mode_code = model.stkcode;
            
            [tempArray addObject:flowModel];
        }
        _pDataList = tempArray;
    }
    [_pTableView reloadData];
}
#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _pDataList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UITableViewCell *cell = nil;
    HZPeiziMoneyFlowModel *model = _pDataList[indexPath.row];
    cell = [HZPeiziMoneyFlowCell cellTableView:tableView indexPath:indexPath cellModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return self.pHeaderView;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (_pDataList.count > 0) {
        return 30;
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    HZPeiziMoneyFlowModel *model = _pDataList[indexPath.row];
    [_pTopView clearData];
    LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterPeiziCodeSearch];
    if (block) {
        block(nil, model.mode_code);
    }
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (HZPeiziHeaderView *)pHeaderView {
    if (!_pHeaderView) {
        _pHeaderView = [HZPeiziHeaderView new];
        [_pHeaderView setTexts:@[@"证券/市值",@"参考盈亏",@"持仓/可用",@"成本价/现价"]];
    }
    return _pHeaderView;
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self endEditing:YES];
}

@end
